/**
 * 超级染色体封装
 */

//基本参数
var RES_DIR = "";           //资源路径
var USE_NATIVE_SOUND = false;        //使用本地的声音
var IS_NATIVE_ANDROID = false;        //本地的安卓
var best = -10000;       //最好的成绩
var score = 0;            //分数
var keyStorage = "bestcjrst";  //存储键值

//初始参数
var stage, //画布
    W = 640, //宽
    H = 960, //高度
    IS_TOUCH,
    SCREEN_SHOW_ALL = false; //显示全部

//游戏参数
var qp_a = 12,
    qp_b = 14,
    qp_c = 25,
    qp_d = 27,
    qp_e = 80,
    qp_f = 50,
    qp_g = 0,
    qp_h = 0,
    COLOR_RED = "#B2151E", //颜色1
    COLOR_GREEN = "#15B26D", //颜色2
    COLOR_PURPLE = "#64549D", //颜色3
    COLOR_YELLOW = "#FEE789", //颜色4
    COLOR_BLUE = "#8ECFF5", //颜色5
    COLOR_PINK = "#F6AAC4", //颜色6
    qp_i,
    qp_j,
    qp_k = null;

var queue;


(function (window) {
    'use strict';
    var game = function () {
        this._init();
    };
    game.prototype = {
        params: {
            //接口
            //interfaceThirdLogin :Config.api.thirdLogin  //第三方登录
        }

        , _init: function () {
            var that = this
                , option = that.params;

            that.initBest();        //初始加载最好成绩
            that.initStage();       //加载舞台
            that.setCanvas();       //设置画布

            //选择预加载
            queue = new createjs.LoadQueue;

            that.loadResourceSound();    //加载声音资源
            that.LoadWelComeSceneRes();  //加载欢迎资源
            that.loadGamesRes();  //加载游戏资源
            that.LoadGameoverSceneRes(); //加载游戏结束资源
            that.progress();        //加载进度条

            queue.on("complete", that.resourceLoadComplete);
            queue.load();
        }

        //初始加载最好成绩
        , initBest: function () {
            best = window.localStorage.getItem(keyStorage) || 0;
        }



        //事件会在页面或图像加载完成后立即发生
        , initStage: function () {
            var that = this,
                option = that.params;

            // step1:创建stage对象,相当于根容器，所有效果均添加在stage上
            stage = new createjs.Stage("stage");

            // step2:兼容手机点击事件，参数根据需要调整
            IS_TOUCH = createjs.Touch.isSupported();
            if (IS_TOUCH) {
                createjs.Touch.enable(stage, true);
                var a = new createjs.Shape;
                a.graphics.f("white").r(0, 0, W, H);
                stage.addChild(a);
            }

            // step3:设置动画帧频
            createjs.Ticker.setFPS(60);
            setTimeout(that.setCanvas(), 100);
            createjs.Ticker.on("tick", stage);
        }


        //设置画布
        , setCanvas: function () {
            var a = stage.canvas,
                b = window.innerWidth,
                c = window.innerHeight - 3;
            if (SCREEN_SHOW_ALL) {
                var d = c;
                b / c > W / H ? b = W * c / H : c = H * b / W;
                a.style.marginTop = (d - c) / 2 + "px";
            } else {
                d = W * c / H, b >= d ? (b = d, stage.x = 0) : stage.x = (b - d) / 2;
            }
            a.width = W;
            a.height = H;
            a.style.width = b + "px";
            a.style.height = c + "px";
        }

        //进度条
        , progress: function () {
            var a = new ProgressBar(0.8 * W, 40);
            a.regX = a.w / 2;
            a.regY = a.h / 2;
            a.x = W / 2;
            a.y = H / 2;
            stage.addChild(a);
            a.forQueue(queue);
        }

        //加载声音资源
        , loadResourceSound: function () {
            var that = this
                , option = that.params;

            SCREEN_SHOW_ALL = true;

            //判断不同的设备加载不同的资源
            USE_NATIVE_SOUND || (IS_NATIVE_ANDROID ? (
                    createjs.Sound.registMySound("do", 0),
                        createjs.Sound.registMySound("re", 2),
                        createjs.Sound.registMySound("me", 4),
                        createjs.Sound.registMySound("fa", 6),
                        createjs.Sound.registMySound("so", 8),
                        createjs.Sound.registMySound("la", 10),
                        createjs.Sound.registMySound("silenttail", 12),
                        queue.loadFile({
                            id: "sound",
                            src: RES_DIR + "audio/all.mp3"
                        })
                ) : (
                    createjs.Sound.alternateExtensions = ["ogg"],
                        queue.installPlugin(createjs.Sound),
                        queue.loadManifest({
                            path: RES_DIR + "audio/",
                            manifest: [
                                {src: "1.mp3", id: "do"},
                                {src: "2.mp3", id: "re"},
                                {src: "3.mp3", id: "mi"},
                                {src: "4.mp3", id: "fa"},
                                {src: "5.mp3", id: "so"},
                                {src: "6.mp3", id: "la"}
                            ]
                        }, false)
                )
            );
        }

        //加载游戏资源
        , loadGamesRes: function () {
            queue.loadManifest({
                path: RES_DIR + "img/",
                manifest: [
                    {src: "step.png", id: "step"},              //步数
                    {src: "bg.jpg", id: "bg"},                  //背景
                    {src: "bluebtn.png", id: "bluebtn"},        //下边蓝色按钮
                    {src: "greenbtn.png", id: "greenbtn"},      //下边绿色按钮
                    {src: "pinkbtn.png", id: "pinkbtn"},        //下边粉色按钮
                    {src: "purplebtn.png", id: "purplebtn"},    //下边紫色按钮
                    {src: "redbtn.png", id: "redbtn"},          //下边红色按钮
                    {src: "yellowbtn.png", id: "yellowbtn"}]    //下边黄色按钮
            }, false);

        }

        //初始欢迎页面加载场景资源
        , LoadWelComeSceneRes: function () {
            queue.loadManifest({
                path: RES_DIR + "img/",
                manifest: [
                    {src: "banner.jpg", id: "banner"},            //banner图片
                    {src: "help.jpg", id: "help"},                //帮助
                    {src: "discription.png", id: "discription"},  //描述
                    {src: "playbtn.png", id: "playbtn"},          //播放按钮
                    {src: "topbtn.png", id: "topbtn"}             //游戏记录
                ]
            }, false);
        }

        //加载游戏结束场景图片
        , LoadGameoverSceneRes: function () {
            queue.loadManifest({
                path: RES_DIR + "img/",
                manifest: [
                    {src: "maxscore.png", id: "maxscore"},      //历史最高
                    {src: "curscore.png", id: "curscore"},      //当前得分
                    {src: "sharebtn.png", id: "sharebtn"},      //分享到朋友圈
                    {src: "gameoverbg.png", id: "gameoverbg"},  //游戏结束背景图片
                    {src: "gameover.png", id: "gameover"}       //游戏结束文字图片
                ]
            }, false);
        }

        //资源加载完成
        , resourceLoadComplete: function () {
            var that = this
                , option = that.param;

            new WelComeScene("#8de9cb",  qp_m, stage);
        }


    };
    window.game = game;
})(window);
